var Product = Vue.component('product', function (resolve, reject) {
    axios.get('views/product/product.html').then(function (res) {
        resolve({
            template: res.data,
            data(){
                return {
                    needAdminRole:CommonUtils.checkRole('admin'),
                    needAliMgrRole:CommonUtils.checkRole("alipay"),
                    loading: false,
                    tableData: [],
                    dialogFormVisible: false,
                    form: {
                        price:0
                    },
                    pageInfo:{
                        currentPage:1,
                        pageSize:10,
                        totalNum:10,
                        srch_EQ_akjAlipayLifeId:[],
                        srch_LIKE_name:"",
                        srch_EQ_delete:false
                    },
                    imageUrl:'',
                    dialogImageUrl: '',
                    dialogVisible: false,
                    editForm:{},
                    showDetailWin:false,
                    detailForm:{
                        name:"",
                        descript:'',
                        akjAlipayLife:{
                            name:''
                        },
                        img:{
                            absPath:""
                        },
                        updateBy:{
                            name:""
                        },
                        createBy:{
                            name:""
                        }
                    },
                    showEditWin:false,
                    allLifes:[],
                    srchLifeIds:[],
                    relateLife:{},
                    fileData:{mark:"product"},
                    formLabelWidth: '120px',
                    disableDetailForm:false,
                    uploadHead:{token:sessionStorage.token},
                    editImageUrl:'',
                    fileAction:ctx+'/file/upload'
                }
            },
            methods:{
                handleImgSuccess(res, file) {
                    if(res.status == '00'){
                        this.imageUrl = URL.createObjectURL(file.raw);
                        this.form.fileId = res.data.id;
                    }else{
                        this.$alert(res.msg, '', {
                            confirmButtonText: '确定'
                        });
                    }
                },
                handleEditImgSuccess(res, file) {
                    if(res.status == '00'){
                        this.editImageUrl = URL.createObjectURL(file.raw);
                        this.editForm.fileId = res.data.id;
                    }else{
                        this.$alert(res.msg, '', {
                            confirmButtonText: '确定'
                        });
                    }
                },
                handleRemove(file, fileList) {
                    console.log(file, fileList);
                },
                imgPreview(file) {
                    this.dialogImageUrl = file.url;
                    this.dialogVisible = true;
                },
                handleCurrentChange: function(currentPage){
                    this.pageInfo.currentPage = currentPage;
                    this.initData();
                },
                initData:function(){
                    var loginUser = JSON.parse(sessionStorage.loginUser);
                    this.loading = true;
                    setTimeout(() => {
                        this.loading = false;
                    }, 3000);

                    if(CommonUtils.checkNull(this.srchLifeIds)){
                        this.pageInfo.srch_EQ_akjAlipayLifeId = CommonUtils.productSrchLifeIds();
                        // this.pageInfo.srch_EQ_akjAlipayLifeId = "";
                    }else{
                        this.pageInfo.srch_EQ_akjAlipayLifeId = this.srchLifeIds;
                    }
                    httpInvoker.get(APIs.product.query,{
                        params:this.pageInfo,
                        paramsSerializer: params => {
                            return Qs.stringify(params, { indices: false })
                        }
                    }).then(res =>{
                        this.loading = false;
                        // console.log(res.data);
                        if(res.status == 200 && res.data.status == '00'){
                            this.allLifes = res.data.data.allLifes;
                            this.tableData = res.data.data.page.content;
                            this.pageInfo.totalNum=res.data.data.page.totalElements;
                        }else{
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }
                    })
                },
                openFormWin(event){
                    if(!CommonUtils.checkRole('alipay')){
                        this.$alert('权限不足', '', {
                            confirmButtonText: '确定',
                        });
                        return;
                    }
                    this.form = {};
                    this.imageUrl='';
                    this.dialogFormVisible=true;
                },
                submitForm(event){
                    this.loading = true;
                    setTimeout(() => {this.loading = false;}, 5000);
                    var submitData = JSON.parse(JSON.stringify(this.form));

                    submitData.price = this.form.price.toString();
                    httpInvoker.post(APIs.product.add,submitData).then(res =>{
                        if(res.status == 200 && res.data.status == '00'){
                            this.dialogFormVisible = false;
                            this.$alert('创建成功', '', {
                                confirmButtonText: '确定',
                                callback: action => {
                                    this.initData();
                                }

                            });
                        }else{
                            this.loading = false;
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }
                    })
                },

                submitEditForm(event){
                    var submitData = {
                        id:this.editForm.id,
                        name:this.editForm.name,
                        descript:this.editForm.descript,
                        price:this.editForm.price.toString(),
                        fileId:this.editForm.fileId?this.editForm.fileId:this.editForm.imgFileId,
                        relateLife:this.editForm.relateLife,
                        activationTime:this.editForm.activationTime.toString()
                    }
                    this.loading = true;
                    setTimeout(() => {this.loading = false;}, 5000);

                    httpInvoker.post(APIs.product.edit,submitData).then(res =>{
                        if(res.status == 200 && res.data.status == '00'){
                            this.showEditWin = false;
                            this.$alert('编辑成功', '', {
                                confirmButtonText: '确定',
                                callback: action => {
                                    this.initData();
                                }

                            });
                        }else{
                            this.loading = false;
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }
                    })
                },

                beforeUpload (file) {
                    const isJPG = file.type === 'image/jpeg';
                    const isPNG = file.type === 'image/png';
                    const isLt2M = file.size / 1024 / 1024 < 2;

                    if (!isJPG && !isPNG) {
                        this.$message.error('上传图片只能是 JPG 或 PNG格式!');
                    }
                    if (!isLt2M) {
                        this.$message.error('上传头像图片大小不能超过 2MB!');
                    }
                    return isLt2M && (isJPG || isPNG);
                },

                updateData(row){
                    if(!CommonUtils.checkRole('alipay')){
                        this.$alert('权限不足', '', {
                            confirmButtonText: '确定',
                        });
                        return;
                    }
                    this.editForm=row;
                    this.editForm.relateLife = row.akjAlipayLife.id;
                    if(CommonUtils.checkNull(row.img)){
                        row.img={};
                        row.img.absPath = "";
                        row.img.id = "";
                    }
                    // this.editForm.fileId=row.img.id;
                    this.editImageUrl = row.img.absPath;
                    this.showEditWin = true;
                },
                cancelEdit(event){
                    this.showEditWin = false;
                    this.initData();
                },
                openDetails(row){
                    this.detailForm=row;
                    this.showDetailWin = true;
                    this.disableDetailForm=true;
                },
                deleteData(row){
                    this.$confirm('删除'+row.name+'，会作废'+row.name+'下面的所有订单，订单作废后无法恢复！确定要删除吗？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        httpInvoker.post(APIs.product.delete,{id:row.id}).then(res =>{
                            if(res.status == 200 && res.data.status == '00'){
                                this.$alert('删除成功', '', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        this.initData();
                                    }

                                });
                            }else{
                                this.$alert(res.data.msg, '', {
                                    confirmButtonText: '确定'
                                });
                            }
                        })
                    }).catch(() => {

                    });
                }
            },

            mounted: function () {
                this.initData();
            },
        })
    })
})